﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DLULearningNetworkModel;
using DLULearningNetwork.Libs;
using DLULearningNetwork.ActionFilterAttributes;

namespace DLULearningNetwork.Controllers
{
    public class AdminHomeController : Controller
    {
        [RequireAdminLogin]
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult NoAccess()
        {
            return View();
        }

        public ActionResult Login()
        {
            return View();
        }

        public ActionResult Logout()
        {
            LibSession.Remove(Constants.ACCOUNT_LOGIN);
            LibCookie.Remove(Constants.EMAIL_LOGIN);
            LibCookie.Remove(Constants.PASSWORD_LOGIN);
            return RedirectToAction("Login");
        }

        [HttpPost]
        public ActionResult Login(Account account, FormCollection frm)
        {
            Account acc = Account.Single(account.Email_s, StringHelper.EncodeMD5(account.Password_s));
            if (acc != null)
            {
                LibSession.Set(Constants.ACCOUNT_LOGIN, acc);
                if (frm["Remember"] != null)
                {
                    LibCookie.Add(Constants.EMAIL_LOGIN, acc.Email_s, DateTime.Now.AddDays(20));
                    LibCookie.Add(Constants.PASSWORD_LOGIN, acc.Password_s, DateTime.Now.AddDays(20));
                }

                return RedirectToAction("Index");
            }

            return View();
        }
    }
}
